<?php
if (!defined('BASEPATH')) {
    exit('No direct script access allowed');
}

class M_media extends CI_Model
{
    
    public function __construct() {
        parent::__construct();
        //initialize db tables data
        $this->tables = $this->config->item('tables', 'ion_auth');
    }
    
    function get() {
        $sql = 'SELECT * FROM '.$this->tables['media'].'
        ORDER BY id DESC';
        return $sql;
    }

    function get_by($id) {
        $sql = 'SELECT '.$this->tables['media'].'.*, '.$this->tables['users'].'.first_name AS uploader FROM '.$this->tables['media'].' 
        JOIN '.$this->tables['users'].' ON '.$this->tables['users'].'.id = '.$this->tables['media'].'.user_id
        WHERE '.$this->tables['media'].'.id = ?';
        $query = $this->db->query($sql, $id);
        if ($query->num_rows() > 0) {
            $result = $query->row_array();
            $query->free_result();
        } else {
            $result = array();
        }
        return $result;
    }

    function save($dataInsert) {
        $query = $this->db->insert($this->tables['media'], $dataInsert);
        return $query;
    }
    
    function update($id, $dataUpdate) {
        $this->db->where('id', $id);
        $update = $this->db->update($this->tables['media'], $dataUpdate);
        return $update;
    }
    
    /*function delete($id_fitur) {
        $this->db->where('id_fitur', $id_fitur);
        $delete = $this->db->delete('fitur');
        return $delete;
    }*/
}
